package server;

import java.io.IOException;
import java.io.StringReader;
import java.util.Scanner;

import javax.xml.bind.JAXBException;

import server.*;

public class BillSysServer extends AbstractServer{

	//Class variables *************************************************
	  
	  /**
	   * The default port to listen on.
	   */
	  final public static int DEFAULT_PORT = 5555;
	  
	  BillSysMySQL data;
	  ServerIF serverUI;
	  
	  //Constructors ****************************************************
	  
	  /**
	   * Constructs an instance of the echo server.
	   *
	   * @param port The port number to connect on.
	   */
	  public BillSysServer(int port, ServerIF serverUI) 
	  {
	    super(port);
	    this.serverUI = serverUI;
	    data = new BillSysMySQL(this.serverUI);
	  }
	  
	  /**
	   * This method tells the server to start listening for connections
	   *
	   */
	  protected void billSysServerStarter()
	  {
		  try 
		    {
		      listen(); //Start listening for connections
		    } 
		    catch (Exception ex) 
		    {
		      displayMsg("ERROR - Could not listen for clients!");
		      System.exit(2);
		    }
	  }
	
	  
	  /**
	   * This method handles any messages received from the client. It checks if
	   * the user has provided a user identification prior of being allowed 
	   * to send messages.
	   *
	   * @param msg The message received from the client.
	   * @param client The connection from which the message originated.
	   */
	  public void handleMessageFromClient(Object msg, ConnectionToClient client)
	  {
		 StringReader message = new StringReader((String) msg);
		 displayMsg(message.toString());
		 if (message != null)
			try {
				data.newTransaction(message);
			} catch (JAXBException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		  
	  }
	  
	  
	  /**
	   * This method allows the user to send a message received to be stored in a 
	   * database.
	   * 
	   * @param msg The message received from the client.
	   * @param client The connection from which the message originated.
	   */
	/*  protected void saveMessageMySQL(String msg)
	  {
		  try {
			data.newTransaction(msg);
		} catch (Exception e) {
			displayMsg("SERVER - BillSysServer - saveMessageMySQL()");
		}
	  }*/
	  
	  /**
	   * This method allows the server to connect to the database. It tests
	   * the connections and requests for data. Method is going to be further
	   * divided. It is just used for testing purposes.
	   * 
	   */
	  protected void connectMySQL()
	  {
		  
				try {
					data.connectMySQL();
				} catch (Exception e) {
					displayMsg("connectMySQL() - Unable to connect");
				}
				try {
					data.getMessagesMySQL();
				} catch (Exception e) {
					displayMsg("getMessagesMySQL() - Unable to get data");
				}
	  }
	  
	  /**
	   * Method called each time a new client connection is
	   * accepted. Makes shore that a user provides a username.
	   * This method does not have to be synchronized since only
	   * one client can be accepted at a time.
	   *
	   * @param client the connection connected to the client.
	   */
	  protected void clientConnected(ConnectionToClient client) {
		  String msg = data.getTest();
			  try
			  {
			  client.sendToClient(msg);
			  }  catch (IOException a){
				  displayMsg("ERROR OCCURRED - clientConnected() method");
			  }
	  }
	  
	  
	    
	  /**
	   * This method overrides the one in the superclass.  Called
	   * when the server starts listening for connections.
	   */
	  protected void serverStarted()
	  {
	    displayMsg
	      ("Server listening for connections on port " + getPort());
	  }
	  
	  /**
	   * This method overrides the one in the superclass.  Called
	   * when the server stops listening for connections.
	   */
	  protected void serverClosed()
	  {
	    displayMsg
	      ("Server has stopped listening for connections.");
	    // The database is closed if the server stops
	    data.closeConnection();
	  }
	  
	  public void displayMsg(String message)
	  {
		serverUI.display(message); 
	  }
	  
	  /**
	   * Closes the server socket and the connections with all clients.
	   *
	   * @exception IOException if an I/O error occurs while closing
	   * the server socket.
	   */
	  public void closeConnections()
	  {
		  try{
			  close();
		  } catch (IOException a){
			  serverUI.display("ERROR - Error occurred while closing");
		  }
	  }
	  
	  
	  
}
